<template>
  <div>
    <div v-model="searchData" style="margin: 10px 0">
      <el-input v-model="searchData.name" style="width: 200px" placeholder="请输入名称"
                suffix-icon="el-icon-search"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="rest">重置</el-button>
    </div>
    <el-button type="primary" @click="addHander" >新增 <i class="el-icon-circle-plus-outline"></i></el-button>
    <div style="margin: 10px 0">
      <div style="padding: 10px 0; border-bottom: 1px dashed #ccc" v-for="item in articles" :key="item.id">
        <div class="pd-10" style="font-size: 20px; color: #3F5EFB; cursor: pointer" @click="$router.push('/front/articleDetail?id=' + item.id)">{{ item.name }}</div>
        <div style="font-size: 14px; margin-top: 10px">
          <i class="el-icon-user-solid"></i> <span>{{ item.user }}</span>
          <i class="el-icon-time" style="margin-left: 10px"></i> <span>{{ item.time }}</span>
        </div>
      </div>
    </div>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增文章信息" :visible.sync="dialogFormVisible" width="60%">
      <el-form :model="form" label-width="80px" size="small">
        <el-form-item label="文章标题">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="文章内容">
          <mavon-editor ref="md" v-model="form.content" :ishljs="true" @imgAdd="imgAdd"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Article",
  data() {
    return {
      form:{},
      current: 0,
      total: 0,
      pageSize: 5,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      searchData: {
        name: '',
      },
     articles:[]
    }
  },
  created() {
    this.load()
  },
  methods: {
    addHander() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() {//新增
      this.request.post("/article/save", this.form).then(resp => {
        console.log(resp)
        if (resp.data) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
          this.load()
        }
      })
    },
    load() {
      this.request.get("/article/page", {
            params: {
              current: this.current,
              pageSize: this.pageSize,
              name: this.searchData.name,
            }
          }
      ).then(resp => {
        console.log(resp)
        this.articles = resp.data.records
        this.total = resp.data.total
      })
    },
    rest(){
      this.searchData.name = ''
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log("每页条数: " + pageSize)
      this.pageSize = pageSize;
      this.load()
    },
    handleCurrentChange(current) {
      console.log("当前页码: " + current)
      this.current = current;
      this.load()
    },
  }
}
</script>

<style>
.headerBg {
  background: #eee !important;
}
</style>